var QQMapWX = require('./qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
   data:{
     latitude:23.099994,
     longitude:113.324520,
     mapw:'100%',
     maph:'20%',
     scale:'18',
     markers:[],
     controls:''
   },
  mapCtx: null,
  onLoad: function () {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'BHOBZ-IGVCI-JKLGX-5P6UT-PWROZ-QYBUD' 
    });
    this.mapCtx = wx.createMapContext('map')
    wx.getSystemInfo({
      success:res=>{
        var mapw = res.windowWidth
        var maph = res.windowHeight
        this.setData({
          maph: maph + 'px',
          controls:[{
            id:1,
            iconPath:'/image/手指.png',
            position:{
              left:10,
              top:maph-50,
              width:30,
              height:20
            },
            clickable:true //可以点击
          }]
        })
      }
  })
  },
  onReady:function(){
    wx.getLocation({
      type:'gcj02',
      success:res=>{
        this.setData({
          longitude:res.longitude,
          latitude:res.latitude
        })
        this.getFood(res.longitude,res.latitude)
      }
    })
  },
  //点击回到初始位置
  bindControlTap(e){
    console.log(e.controlId)
    if(e.controlId ===1){
      this.mapCtx.moveToLocation()
    }
  },
  //滑动获取周围的餐厅
  bindRegionChange(e){
   if(e.type ==='end'){
     this.mapCtx.getCenterLocation({
       success:res=>{
         this.getFood(res.longitude,res.latitude)
       }
     })
   }
  },
  getFood: function (longitude, latitude){
    qqmapsdk.search({
      keyword:'宠物医院',
      location:{
        longitude: longitude,
        latitude: latitude
      },
      success:res=>{
        console.log('mapmapmpampampampa',res)
        var mark = []
        for(let i in res.data){
          mark.push({
            iconPath:'/image/宠物医院.png',
            title: res.data[i].title,
            id:i,
            longitude:res.data[i].location.lng,
            latitude: res.data[i].location.lat,
          })
        }
        mark.push({
          iconPath: '/image/people.png',
          id: res.data.length,
          longitude: longitude,
          latitude: latitude,
        })
        this.setData({
          markers:mark
        })
      },
      fail:res=>{
        console.log(res)
      }
    })
  },
})
